from flask import Flask,request,current_app,make_response,redirect,abort,render_template,url_for,session
from flask_script import Manager
from flask_bootstrap import Bootstrap #未安装 pip install Flask-Bootstrap
from flask_moment import Moment #未安装pip install flask-moment
from datetime import datetime #时间库，与Moment一起用
#pip install flask-wtf 处理web表单，对独立的WTForms包进行了包装偏大成到flask
from flask_wtf import Form
from wtforms import StringField,SubmitField
from wtforms.validators import Required
#pip install flask-sqlalchemy
from flask_migrate import Migrate,MigrateCommand#pip install flask-migrate 数据库迁移仓库
#pip install flask-mail
from flask_mail import Mail

app = Flask(__name__)
#manager = Manager(app)
#moment = Moment(app) /在服务器上使用UTC时间，把时间单位发送给Client转换成当地时间然后渲染

#app.config['SECRET_KEY'] = 'hard to guess string'
#app,config字典存储框架、扩展和程序本身的配置变量。

@app.route('/')
def hello_world():
    return render_template('index.html')

@app.route('/useragent/')
def useragent():
    user_agent = request.headers.get('User-Agent')
    return '<p>your browser is %s</p>' % user_agent

@app.route('/response')
def response():
    response = make_response('<h1>This document carries a cookie!</h1>')
    response.set_cookie('answer','42')
    return  redirect('/useragent')

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name = name)

if __name__ == '__main__':
    app.run(debug=True)

'''
app_ctx = app.app_context()
app_ctx.push()
print(current_app.name)
app_ctx.pop()
print(app.url_map)'''

class NameForm(Form):
    name = StringField('What is your name?',validators=[Required()])
    submit = SubmitField('Submit')

#mail功能
import os
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
#不把邮箱贴密码写进脚本，从环境中读取
'''使用方法
from flask_mail import Mail
mail = Mail(app)
'''